import { resolve } from 'node:path';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJSX from '@vitejs/plugin-vue-jsx';

import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { /* ElementPlusResolver */ AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import { createStyleImportPlugin, AndDesignVueResolve } from 'vite-plugin-style-import';
// import Icons from 'unplugin-icons/vite';
// import IconsResolver from 'unplugin-icons/resolver';

import compressPlugin from 'vite-plugin-compression';

import { viteMockServe } from 'vite-plugin-mock';

function join(path: string) {
  return resolve(__dirname, path);
}

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  console.log(`command => ${command}`);
  console.log(`mode => ${mode}`);
  return {
    base: '/link/editor/',
    resolve: {
      alias: {
        '@views': join('./src/views'),
        '@components': join('./src/components')
      }
    },
    server: {
      // port: 3000
    },
    plugins: [
      vue(),
      vueJSX(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        dts: join('./src/auto-import.d.ts'),
        resolvers: [
          /* ElementPlusResolver() */
          AntDesignVueResolver()
        ]
      }),
      Components({
        resolvers: [AntDesignVueResolver()],
        dirs: ['src/components'],
        directoryAsNamespace: true,
        dts: join('./src/components.d.ts')
      }),
      createStyleImportPlugin({
        resolves: [AndDesignVueResolve()]
      }),
      viteMockServe({
        mockPath: 'server',
        prodEnabled: false,
        injectCode: `
          import { setupProMockServer } from '../server/app';
          setupProMockServer();
        `
      }),
      compressPlugin({
        threshold: 1024 * 50,
        deleteOriginFile: false,
        algorithm: 'gzip',
        ext: '.gz'
      })
    ],
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true
        }
      }
    },
    build: {
      outDir: join('./dist/link/editor/'),
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,
          pure_funcs: ['console.log']
        }
      },
      // sourcemap: 'inline',
      reportCompressedSize: false,
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ['vue', 'vue-router', 'pinia'],
            antd: ['ant-design-vue']
          }
        }
      }
    }
  };
});
